<?php
	/*==================================================================*/
	/*		文件名:Column.class.php                              */
	/*		概要: 分类管理.                	       	    */
	/*==================================================================*/
	class ColumnModel extends  CatModel{	
		//==========================================
		// 函数:  __construct($showError=TRUE)
		// 功能: 初使化对象成员
		// 参数: $showError用户设置是否显示错误报告提示
		// 返回: 无
		//==========================================
		function __construct(){
			parent:: __construct();
			$this->tabName = TAB_PREFIX."column";
			$this->gtabName=TAB_PREFIX."picture";
			$this->fieldList=array("id","path", "title", "description","display","picid", "ord");
		}
		//==========================================
		// 函数:  getTree()
		// 功能: 从数据表中获取分类的结构树
		// 参数: 无
		// 返回: 无限分类的分类对结构数组
		//==========================================
		function getTree(){
			$sql="SELECT id, path,concat(path,'-',id) AS absPath, title,ord,display, description FROM {$this->tabName} ORDER BY absPath, id";
			pr($sql);
			$result=$this->mysqli->query($sql);
			$record=array();
			while($row=$result->fetch_assoc()){
				$record[]=$row;
			}
			return $record;
		}

		//==========================================
		// 函数:  validateForm()
		// 功能: 对用户添加的相册内容进行验证
		// 参数: 无
		// 返回: true或false
		//==========================================
		function validateForm(){
			$result=true;
			if(!Validate::required($_POST['title'])) {
				$this->messList[] = "栏目名称不能为空.";
				$result=false;
			}
			if(Validate::match($_POST['title'], "|[\\\/\'\"]|")) {
				$this->messList[] = "栏目名称含有非法字符, 不能包含\\ / ' \"等字符.";
				$result=false;
			}
//			if(!Validate::required($_POST['picid'])) {
//				$this->messList[] = "必须为栏目选择一张图片.";
//				$result=false;
//			}
			if(!Validate::checkLength($_POST['description'], 200)) {
				$this->messList[] = "栏目描述不能超过100个汉字.";
				$result=false;
			}

			if(!$result){
				$this->messList[] = "栏目添加失败.";
			}
			return  $result;
		}

		function parseTree() {	
			$option = $this->getTree();
			$htmlStr='';
			$i=0;
			foreach($option as $value) {
				$class=($i++%2==0)?'light-row':'dark-row';  //隔行换样式
				$htmlStr .= '<li class='.$class.'>';
				if($value["path"]=="0-1"){
					$input ='<input type="text" style="width:20px;height:20px;" name="ords[]" value="'.$value["ord"].'">';
					$input.='<input type="hidden" name="ids[]" value="'.$value["id"].'">';
				}else{
					$input='&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
				}
				$title = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',count(explode('-',$value['absPath']))-2).$input. '|--<a href="admin.php?m=article&a=edit&pid='.$value["id"].'">'.$value['title'].'</a>';
				$htmlStr .= '<span class="col_width2">'.$title.'</span>';
				if($value["id"]>1){
					$htmlStr .= '【<a href="admin.php?m=column&a=mod&id='.$value["id"].'">修改</a>】';
					$htmlStr .= '【<a href="admin.php?m=column&a=del&id='.$value["id"].'" onclick=\'return confirm("确定要删除'.$value['title'].'及子分类吗？")\'>删除</a>】';
					if($value['display']=="1"){
						$htmlStr .= '【<span style="cursor:pointer;"  onclick="dis(this, '.$value['id'].')">正常</span>】';
					}else{
						$htmlStr .= '【<span   style="cursor:pointer;background:#ccc;" onclick="dis(this, '.$value['id'].')">隐藏</span>】';
					}
				}
				$htmlStr .= '</li>';
			}
			return $htmlStr;
		}
		//改变显示顺序
		function order($post){
			$stmt=$this->mysqli->prepare("UPDATE {$this->tabName} SET ord=? WHERE id=?");
			$stmt->bind_param('ii', $ord, $id);
			$affected_rows=0;
			for($i=0; $i<count($post['ids']); $i++){
				$id=$post["ids"][$i];
				$ord=$post["ords"][$i];
				$stmt->execute();
				$affected_rows+=$stmt->affected_rows;
			}
			if($affected_rows > 0) {
				$this->messList[] = "类别顺序修改成功.";
				return true;
			}else{
				$this->messList[] = "没有需要修改的类别顺序.";
				return false;
			}	
		}

		//设置分类是否显示
		function setDis($id, $value){
			$sql="update {$this->tabName} set display='{$value}' where id={$id}";

			if($this->mysqli->query($sql)){
				return true;
			}else{
				return false;
			}
		}
		function find($id) {
			$fieldList=array();
			foreach($this->fieldList as $key=>$value){
				$fieldList[$key]="tab1.".$value;
			}	
			$filed=join(",", $fieldList);
			$sql="select {$filed} from {$this->tabName} as tab1 where  tab1.id={$id}";
			pr($sql);
			$result=$this->mysqli->query($sql);
			if($result && $result->num_rows >0){
				return $result->fetch_assoc();
			}else{
				return null;
			}
	
		}
		function getColumnList($id){
			$field=join(",", $this->fieldList);
			$sql="SELECT {$field} FROM {$this->tabName} WHERE display=1 and path='".$this->getCatPath($id)."' ORDER BY ord";
			//pr($sql);
			$result=$this->mysqli->query($sql);
			$record=array();
			while($row=$result->fetch_assoc()){
				$record[]=$row;
			}
			return $record;
		}

	
		function getAllColumn($id){
			$field=join(",", $this->fieldList);
			$sql="SELECT {$field} FROM {$this->tabName} WHERE display=1 and path='".$this->getCatPath($id)."' ORDER BY ord";
			//pr($sql);
			$result=$this->mysqli->query($sql);
			$record=array();
			$img=new ImageModel();
			$art=new ArticleModel();
			while($row=$result->fetch_assoc()){
				$row["picid"]=$img->getPicPath($row["picid"]);
				$row["subCol"]=$this->getColumnList($row["id"]);
				$row["art"]=$art->findAll(array("id, title,author,postTime"),0, 10,"where pid='".$row["id"]."' and audit=1");
				$record[]=$row;
			}
			return $record;
		}
		function getColumn($id, $offset, $num){
			$img=new ImageModel();
			$art=new ArticleModel();
			$sql="SELECT id,path, title, description,picid FROM {$this->tabName} WHERE id={$id}";
		
			$result=$this->mysqli->query($sql);
			$record=$result->fetch_assoc();
			$record["picid"]=$img->getPicPath($record["picid"]);
			$record["art"]=$art->findAll(array("id, title,author,postTime"),$offset, $num, "WHERE  pid={$id} and audit=1");
			return $record;
		}
		//当前位置
		function getLocation($path){
			$sql="SELECT id,path, title, description FROM {$this->tabName} WHERE id in (".str_replace("-", ",", $path).")";
			$result=$this->mysqli->query($sql);
			$record=array();
			while($row=$result->fetch_assoc()){
				$record[]=$row;
			}
			return $record;
		}
 
	}

